package hu.bme.mit.obp.seammodel.ui;

import hu.bme.mit.inf.seample.gen.SeamGenerationDescriptor;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IPageChangedListener;
import org.eclipse.jface.dialogs.PageChangedEvent;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
import org.eclipse.wst.common.frameworks.internal.operations.IProjectCreationPropertiesNew;
import org.jboss.tools.common.model.ui.util.ExtensionPointUtils;
import org.jboss.tools.seam.internal.core.project.facet.ISeamFacetDataModelProperties;
import org.jboss.tools.seam.ui.SeamGuiPlugin;
import org.jboss.tools.seam.ui.SeamUIMessages;
import org.jboss.tools.seam.ui.internal.project.facet.SeamInstallWizardPage;
import org.jboss.tools.seam.ui.wizard.SeamProjectWizard;

/**
 * Generates a Seam Application Skeleton. Uses the Default Seam Web Project
 * Wizard provided by JBoss Tools.
 * 
 * @author Mark Czotter
 * 
 */
@SuppressWarnings("restriction")
public class GenerateSeamWebProjectAction extends
		AbstractSeamApplicationSkeletonAction {

	@Override
	public void run(IAction action) {
		String ejbProjectName = getGeneratorModel().getProjectName() + "-ejb";
		String earProjectName = getGeneratorModel().getProjectName() + "-ear";
		String domainPackage = getGeneratorModel().getDomainPackageName();
		String businessPackage = getGeneratorModel().getBusinessPackageName();
		IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot();
		IProject project = ws.getProject(getGeneratorModel().getProjectName());
		IProject ejbProject = ws.getProject(ejbProjectName);
		IProject earProject = ws.getProject(earProjectName);
		if (!project.exists() && !ejbProject.exists() && !earProject.exists()) {
			INewWizard w = ExtensionPointUtils.findNewWizardsItem(
					SeamGuiPlugin.PLUGIN_ID,
					"org.jboss.tools.seam.ui.wizards.SeamProjectWizard" //$NON-NLS-1$
			);
			if (w != null) {
				SeamProjectWizard wizard = (SeamProjectWizard) w;
				wizard.init(PlatformUI.getWorkbench(), null);
				WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getShell(), wizard);
				// create the dialog first (needed for manipulate with the preferences)
				dialog.create();
				// other attributes from the genModel is setted when the page changes
				dialog.addPageChangedListener(new IPageChangedListener() {
					boolean firstTime = true;
					
					@Override
					public void pageChanged(PageChangedEvent event) {
						if (event.getSelectedPage() instanceof SeamInstallWizardPage) {
							if (firstTime) {
								SeamInstallWizardPage page = (SeamInstallWizardPage) event.getSelectedPage();
								firstTime = false;
								IDataModel model = page.getConfig();
								model.setProperty(ISeamFacetDataModelProperties.ENTITY_BEAN_PACKAGE_NAME, getGeneratorModel().getDomainPackageName());
								model.setProperty(ISeamFacetDataModelProperties.SESSION_BEAN_PACKAGE_NAME, getGeneratorModel().getBusinessPackageName());
								page.setConfig(model);
							}
						}
					}
				});
				IDataModel model = wizard.getDataModel();
				if (model != null) {
					// set the initial project settings (project name can be setted from genModel)
					model.setProperty(IProjectCreationPropertiesNew.PROJECT_NAME, getGeneratorModel().getProjectName());
					dialog.open();
					if (dialog.getReturnCode() == Dialog.OK) {
						// get the seam web project
						SeamInstallWizardPage page = (SeamInstallWizardPage) wizard.getPage(SeamUIMessages.SEAM_INSTALL_WIZARD_PAGE_SEAM_FACET);
						IDataModel config = page.getConfig();
						ejbProjectName = (String) config.getProperty(ISeamFacetDataModelProperties.SEAM_EJB_PROJECT);
						earProjectName = (String) config.getProperty(ISeamFacetDataModelProperties.SEAM_EAR_PROJECT);
						domainPackage = (String) config.getProperty(ISeamFacetDataModelProperties.ENTITY_BEAN_PACKAGE_NAME);
						businessPackage = (String) config.getProperty(ISeamFacetDataModelProperties.SESSION_BEAN_PACKAGE_NAME);
						ejbProject = ResourcesPlugin.getWorkspace().getRoot().getProject(ejbProjectName);
						SeamGenerationDescriptor descriptor = new SeamGenerationDescriptor(getGeneratorModel(), ejbProject, earProjectName, domainPackage, businessPackage);
						doGenerateJob(descriptor);
					}
				}
			}		
		} else {
			SeamGenerationDescriptor descriptor = new SeamGenerationDescriptor(getGeneratorModel(), ejbProject, earProjectName, domainPackage, businessPackage);
			doGenerateJob(descriptor);
		}
	}

}
